/**

 * 
 */
package com.deep.test.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

/**
 * @author hudepin
 *
 * 2018年4月3日下午2:29:07
 */
public class XmlSmaple {
	private static Document readXml(String filePath){
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(filePath);
			return doc;
		} catch (Exception e) {
			return null;
		}
	}
	
	private static void writeXml(Document doc,String outputFilePath){
		TransformerFactory tsf = TransformerFactory.newInstance();
		try {
			Transformer tf = tsf.newTransformer();
			String parent = new File(outputFilePath).getParent();
			File f = new File(parent);
			if(!f.exists()){
				f.mkdirs();
			}
			 OutputStream os = new FileOutputStream(new File(outputFilePath)); 
			 tf.setOutputProperty(OutputKeys.INDENT, "yes");
			 tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			 DOMSource ds =new DOMSource(doc);
			 StreamResult sr = new StreamResult(os);
			 tf.transform(ds, sr);
			 os.flush();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			
		}
	}

}
